package com.sctv.news.sjd.ui.utils;

import android.content.Context;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.SharedPreferencesUtil;
import com.sctv.news.sjd.application.Cache;

import java.util.ArrayList;
import java.util.List;

public class ReadNewsHelper {
    public static ReadNewsHelper instance;
    public static final String READ_LIST = "read_news_list";
    public static final String NEWS_TYPE = "news";
    //    public static final String TOPIC_TYPE = "topic";
//    public static final String LIVE_TYPE = "live";
    //    public static final String NEWS_TYPE = "news";
    private List<OnNewsReadListener> readListenerList;
    private Context ctx;
    private Gson gson;

    private Gson initGson() {
        if (gson == null) {
            gson = new Gson();
        }
        return gson;
    }

    public static ReadNewsHelper getInstance(Context context) {
        if (null == instance) {
            instance = new ReadNewsHelper(context);
        }
        return instance;
    }

    /**
     * 初始化缓存数据
     *
     * @param context
     */
    private ReadNewsHelper(Context context) {
        ctx = context;
        readListenerList = new ArrayList<>();
        String param = (String) SharedPreferencesUtil.getParam(ctx, READ_LIST, "[]");
        List<String> readList = initGson().fromJson(param, new TypeToken<List<String>>() {
        }.getType());
        if (ListUtils.isListValued(readList)) {
            Cache.getInstance().setReadNewsLst(readList);
        }
    }

    /**
     * 设置新闻为已读
     *
     * @param id 新闻id
     */
    public void onNewsRead(String id) {
        String param = (String) SharedPreferencesUtil.getParam(ctx, READ_LIST, "[]");
        List<String> readList = initGson().fromJson(param, new TypeToken<List<String>>() {
        }.getType());
        if (!ListUtils.isListValued(readList)) {
            readList = new ArrayList<>();
        }
        if (!isNewsRead(id)) {
            readList.add(id);
            if (ListUtils.isListValued(readList)) {
                String data = initGson().toJson(readList);
                SharedPreferencesUtil.setParam(ctx, READ_LIST, data);
                Cache.getInstance().setReadNewsLst(readList);
                if (ListUtils.isListValued(readListenerList)) {
                    for (int i = 0; i < readListenerList.size(); i++) {
                        readListenerList.get(i).OnSetReadSuccess(id);
//                        readListenerList.remove(i);
                    }
                }
            }
        }
    }

    /**
     * 判断新闻是否已读
     *
     * @param id 新闻id
     * @return
     */
    public boolean isNewsRead(String id) {
        List<String> readList = Cache.getInstance().getReadNewsLst();
        if (ListUtils.isListValued(readList)) {
            return readList.contains(id);
        } else {
            return false;
        }
    }

    /**
     * 清空已读缓存
     */
    public void cleanSaveRead() {
        List<String> readList = new ArrayList<>();
        String data = initGson().toJson(readList);
        SharedPreferencesUtil.setParam(ctx, READ_LIST, data);
        Cache.getInstance().setReadNewsLst(readList);
    }

    /**
     * 注册设置已读监听
     *
     * @param readListener
     */
    public void registerReadListener(OnNewsReadListener readListener) {
        if (null == readListenerList) {
            readListenerList = new ArrayList<>();
        }
        if (!readListenerList.contains(readListener)) {
            this.readListenerList.add(readListener);
        }
    }

    /**
     * 注销已读监听
     *
     * @param readListener
     */
    public void unRegisterReadListener(OnNewsReadListener readListener) {
        if (null != readListenerList && readListenerList.contains(readListener)) {
            readListenerList.remove(readListener);
        }
    }

    public interface OnNewsReadListener {
        void OnSetReadSuccess(String newsId);
    }
}
